package com.sistema.vendas;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;


public class HibernateUtil {
	private static SessionFactory session = buildSessionFactory();
	private static ServiceRegistry serviceRegistry;
	private static SessionFactory buildSessionFactory(){
		try{
			Configuration cfg = new Configuration();
			cfg.configure("hibernate.cfg.xml");
			serviceRegistry = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
			session = cfg.buildSessionFactory(serviceRegistry);
			System.out.println("Conectou! ");
			return session;
			
		}catch (Throwable e){
			System.out.println("Deu pau manolo!\n " + e);
			throw new ExceptionInInitializerError();
			
		}
		
	}
	public static SessionFactory getSession(){
		return session;
		
	}
}
